package com.b2bpsssm.controller.capital;

import com.b2bpsssm.common.R;
import com.b2bpsssm.pojo.Other_Expenses;
import com.b2bpsssm.service.OtherExpensesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@Api(value = "其他支出数据测试",tags = "其他支出API")
public class OtherExpensesController {
   @Autowired
    private OtherExpensesService otherExpensesService;

    //      查询其他支出信息 有分页
    @RequestMapping(value = "/other_expenses/queryOtherExpensesList",method = {RequestMethod.GET},produces = "application/json;charset=utf-8")
    @ApiOperation(value = "其他支出信息  ",notes = "  查询其他支出信息 有分页",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
    })
    @ResponseBody
    public R queryOtherExpensesList(Integer page, Integer limit){
        // 调用Service层进行业务逻辑处理
        return otherExpensesService.queryOtherExpensesList(page,limit);
             }

    //    以收支类型关键字 时间范围 进行关键字查询
    @RequestMapping(value = "/other_expenses/selOtherExpensesLikeType",method = RequestMethod.GET)
    @ApiOperation(value = "其他支出信息  ",notes = "  以收支类型关键字 时间范围 进行关键字查询",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
            @ApiImplicitParam(name = "other_expense_creation_time1", value = "开始时间", required = true),
            @ApiImplicitParam(name = "other_expense_creation_time2", value = "结束时间", required = true),
            @ApiImplicitParam(name = "other_expense_payment_type", value = "收支类型", required = true),
    })
    @ResponseBody
    public R selOtherExpensesLikeType(Integer page, Integer limit, String other_expense_creation_time1, String other_expense_creation_time2, String other_expense_payment_type) {

            return  otherExpensesService.selOtherExpensesLikeType(page,limit,other_expense_creation_time1,other_expense_creation_time2,other_expense_payment_type);

        }

  //添加其他支出
   @RequestMapping(value ="/other_expenses/addOtherExpenses",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
   @ApiOperation(value = "其他支出信息  ",notes = "添加其他支出",httpMethod = "POST",response = R.class)
   @ApiImplicitParams({
           @ApiImplicitParam(name = "other_expense_bills", value = "单据编号", required = true),
           @ApiImplicitParam(name = "other_expense_payment_type", value = "收支类型", required = true),
           @ApiImplicitParam(name = "other_expense_clearing_form", value = "结算方式", required = true),
           @ApiImplicitParam(name = "other_expense_money", value = "金额", required = true),
           @ApiImplicitParam(name = "other_expense_creation_time", value = "创建时间" , required = true),
           @ApiImplicitParam(name = "shop_id", value = "门店ID", required = true),
           @ApiImplicitParam(name = "type_of_expense", value = "费用分类", required = true),
   })
   @ResponseBody
   public R addOtherExpenses(@RequestBody Other_Expenses other_expenses) {

   return  otherExpensesService.addOtherExpenses(other_expenses);


   }
//    @RequestMapping(value = "/other_expenses/accessory",method = RequestMethod.POST)
//    @ResponseBody
//    public R updateOtherExpensesAccessory(@Validated Other_Expenses other_expenses, HttpServletRequest request) throws IOException {
//        R r = new R();
//        if (other_expenses.getOther_expense_accessory().isEmpty()) {
//            System.out.println("上传失败");
//            r.setCode(500);
//            r.setMassage("上传文件不能为空--上传失败");
//        } else {
//            System.out.println("开始上传");
//            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
//            File file = new File(uploadPath);
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            MultipartFile multipartFile = other_expenses.getOther_expense_accessory();
//            String fileName = multipartFile.getOriginalFilename();
//            File newFile = new File(uploadPath + fileName);
//            System.out.println("newFile=" + newFile);
//            FileCopyUtils.copy(multipartFile.getBytes(), newFile);
//            r.setCode(200);
//            r.setMassage("上传成功");
//        }
//        return r;
//    }
    //修改信息
    @RequestMapping(value = "/other_expenses/updateOtherExpenses",method = RequestMethod.POST)
    @ApiOperation(value = "其他支出信息  ",notes = "修改信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "other_expense_bills", value = "单据编号", required = true),
            @ApiImplicitParam(name = "other_expense_payment_type", value = "收支类型", required = true),
            @ApiImplicitParam(name = "other_expense_clearing_form", value = "结算方式", required = true),
            @ApiImplicitParam(name = "other_expense_money", value = "金额", required = true),
            @ApiImplicitParam(name = "other_expense_creation_time", value = "创建时间" , required = true),
            @ApiImplicitParam(name = "shop_id", value = "门店ID", required = true),
            @ApiImplicitParam(name = "type_of_expense", value = "费用分类", required = true),
    })
    @ResponseBody
    public R updateOtherExpenses(@RequestBody Other_Expenses other_expenses){

        return otherExpensesService.updateOtherExpenses(other_expenses);
    }

    // 删除信息
    @RequestMapping(value = "/other_expenses/delOtherExpensesByNumber",method = RequestMethod.POST)
    @ApiOperation(value = "其他支出信息  ",notes = "删除信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "other_expense_bills", value = "单据编号", required = true),
    })
    @ResponseBody
    public R delOtherExpensesByNumber(Other_Expenses other_expense_bills){

        return  otherExpensesService.delOtherExpensesByNumber(other_expense_bills);
    }


   }
